<template>
    <div class="city_container" >
        <div v-if="seen == false">
          <v-header :current-city="current_city"></v-header>
          <v-banner :slider-data="banneRList"></v-banner>
          <v-coombo :combo-data="combo_List"></v-coombo>
          <div class="driving__school">
              <section class="coombo_title">
               附近练车点
              </section>
              <section>
                   <v-drivingschool :driving-data="drivingdata" :ishow="false"></v-drivingschool>
              </section>
          </div>
        </div>
        <v-spinner :seen="seen"></v-spinner>
    </div>
</template>
<script>
    import Sign_head from '../sign_head/sign_head.vue';//导入组建./代表当前项目，../代表上一级项目
    import Banner from '../city/banner/banner.vue';//导入组建./代表当前项目，../代表上一级项目
    import Coombo from '../city/coombo/coombo.vue';
    import Drivingschool from '../city/drivingschool/drivingschool.vue';
    import Spinner from '../component/spinner/spinner.vue';
    export default {
      data() {
            return {
                combo_List:null, //banner列表
                banneRList:null, //banner列表
                lat:'',
                lng:'',
                current_city:"",//当前城市
                open_nowList:"",
                drivingdata:"",
                city_code:"",
                seen:true,
            }
      },
      created(){//通过h5地理定位接口转化获取当前城市
          let that = this;
          if(navigator.geolocation){
             let url = window.location.host.substring(0,8);
             if(url == "https://"){
              console.log("支持定位");
                   navigator.geolocation.getCurrentPosition(that.showPosition,that.locationError)
             }else{
              console.log("不支持定位");
               this.lat = 39.984154,this.lng=116.307490;
               this.getCity();
             }
             // console.log(url);
          }else{
             this.lat = 39.984154,this.lng=39.984154
             this.getCity();
          }
      },
      methods:{
              getCity:function(){
                  let that = this;
                  let lat = this.lat;
                  let lng = this.lng;
                    fetch("http://appsys.goxueche.com/App/Info/index.shtml?debug=1&method=location&action=constant&location="+lat+","+lng,{method: 'POST'}).then(function(response){
                    return response.json();
                  }).then(function(data){

                     that.current_city = data.data.city;
                   //检查定位城市是否已经开通
                     that.getOpencity()
                  })
              },
              //获取开通城市
              getOpencity:function(){
                    let that = this;
                    fetch("http://appsys.goxueche.com/App/Info/index/debug/1/action/user/method/open_city.shtml").then(function(response){
                    return response.json();
                  }).then(function(data){
                      that.open_nowList = data.data.open_now;
                      that.checkCity();
                  })
              },
              checkCity:function(){
                    let that = this;
                    console.log(this.open_nowList);
                    console.log(this.current_city);
                    this.open_nowList.map(function(data,index){
                        if(that.current_city.indexOf(data.name) != -1){
                            that.current_city = data.name;
                            that.city_code = data.code;
                            that.signIndex();
                            that.getDringschool();
                        }
                    })
              },
              showPosition:function(position){
                    this.lat = position.coords.latitude;
                    this.lng = position.coords.longitude;
                    this.getCity();
                    console.log(this.lat);
              },
              locationError:function(error){
                      this.lat = 0,this.lng=0
                      this.getCity();
              },
              //获取驾校信息
              getDringschool:function(){
                  let that = this;
                  let city_code = this.city_code;
                  let lat = this.lat;
                  let lng = this.lng;
                  fetch("http://appsys.goxueche.com/App/Info/index.shtml?debug=1&method=index&action=driving&city_code=" + city_code + "&lat=" + lat + "&lng=" + lng+"&source=web").then(function(response){
                    return response.json();
                  }).then(function(data){
                        let view_data = data.data;
                       if(data.code == 0){
                            that.seen=false;  //获取到全部数据之后关闭loadding
                            that.drivingdata = view_data.driving_data;
                             
                          } 
                  })
              },
              // 获取首页信息3
              // https://api.goxueche.com//App/Info/index.shtml?debug=1&method=index&action=sign&city_code=C031
              // http://appsys.goxueche.com/App/Info/index.shtml?debug=1&method=index&action=sign&city_code="+this.city_code+"&source=weixin
              signIndex:function(){
                   let that = this;
                   console.log("2222");
                 fetch("https://api.goxueche.com//App/Info/index.shtml?debug=1&method=index&action=sign&city_code="+this.city_code).then(function(response){
                return response.json();
                 }).then(function(data){
                     let view_data = data.data;
                     let combo_data = view_data.combo_data;
                     let slide_data = view_data.slider_data;
                     that.banneRList = slide_data; 
                    
                     that.combo_List = combo_data;

            })
              }
      },
      mounted(){

            // let that = this;
            // let city_code = this.city_code;
            // console.log("22");
            // fetch("http://appsys.goxueche.com/App/Info/index.shtml?debug=1&method=index&action=sign&city_code="+city_code+"&source=weixin").then(function(response){
            //     return response.json();
            // }).then(function(data){
            //          let view_data = data.data;
            //          let combo_data = view_data.combo_data;
            //          let slide_data = view_data.slider_data;
            //          that.banneRList = slide_data; 
            //          that.combo_List = combo_data;

            // })
      },
      components: {
        'v-header': Sign_head,//注册组建，调用的时候要用大写
        'v-banner' : Banner,
        'v-coombo' : Coombo,
        'v-drivingschool' : Drivingschool,
        'v-spinner' : Spinner,
      }

    }

</script>

<style lang="scss" scoped>
   @import 'src/common/styles/mixins';
   .city_container{
    width: 100%;
   }
    .driving__school{
    background: #f0f0f0;
    padding: px2rem(15) px2rem(20);
       .coombo_title{
        background: #ffffff;
        font-size: px2rem(28);
        color: #666666;
        padding: px2rem(15) px2rem(30);
      }
   }
</style>
<!-- //动态绑定伏组件中的data属性，用定义的名字来接 -->
<!-- 父－－－子组建通信 -->
<!--     父组件中 动态绑定父组件中的data属性，在子组建中通过props来接，可以自定义属性名 -->